package com.atguigu.tingshu.common;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import jakarta.annotation.Nullable;

import java.util.Collection;

/**
 * 自定义断言类，接受ResultCodeEnum枚举类型的异常
 *
 * @author Haoran Sun
 * @since 2024/08/28
 */


public class Assert {

    public static void notNull(@Nullable Object object, ResultCodeEnum resultCodeEnum) {
        if (object == null) {
            throw new GuiguException(resultCodeEnum);
        }
    }

    public static void notNull(@Nullable Result<?> result, ResultCodeEnum resultCodeEnum) {
        if(result == null){
            throw new GuiguException(resultCodeEnum);
        }
        if(result.getCode() != 200){
            throw new GuiguException(resultCodeEnum);
        }
        if(result.getData() == null){
            throw new GuiguException(resultCodeEnum);
        }
    }


    public static void notEmpty(@Nullable Collection<?> object, ResultCodeEnum resultCodeEnum) {
        if (object == null || object.isEmpty()) {
            throw new GuiguException(resultCodeEnum);
        }
    }

    public static void isTrue(@Nullable Boolean object , ResultCodeEnum resultCodeEnum){
        if(Boolean.FALSE.equals(object)){
            throw new GuiguException(resultCodeEnum);
        }
    }


}
